import { $wuxSelect } from '../../component/wux/index'
var common = require('../../lib/common.js');
var url = require('../../config/url.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: -1,
    oneTypeId: -1,
    twoTypeId: -1,
    oneTypeName: '',
    twoTypeName: '',
    oneType: {
      value: [],
      title: [],
      options: [
      ],
    },
    twoType: {
      value: [],
      title: [],
      options: [
      ],
    },
    videoList: [],
    // 上传链接
    uploadUrl: url.domain + '/Index/Upload/video',
    content: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ id: options.id });
    wx.setNavigationBarTitle({
      title: '文章更新'
    })
    this.getAritcle();
  },
  /**
   * 获取文章详情
   */
  getAritcle() {
    common.post(url.getMyArticleDetail, { id: this.data.id }, (data) => {
      let videoList = [];
      if (data.video != null) {
        videoList.push({
          uid: 0,
          status: 'done',
          url: data.video,
        })
      }
      this.setData({
        content: data.content,
        oneTypeId: data.one_type_id,
        twoTypeId: data.two_type_id,
        oneTypeName: data.one_type_name,
        twoTypeName: data.two_type_name,
        videoList
      });
      this.getOneType();
    });
  },
  /**
   * 获取一级分类
   */
  getOneType() {
    let oneType = {};
    oneType.value = this.data.oneTypeId != -1 ? this.data.oneTypeId : '';
    oneType.title = this.data.oneTypeId != -1 ? this.data.oneTypeName : '';
    oneType.options = [];
    common.post(url.getOneType, {}, (res) => {
      for (let i = 0; i < res.length; i++) {
        oneType.options.push({
          title: res[i].name,
          value: res[i].id + '',
        });
      }
      this.setData({ oneType });
      this.getTwoType();
    });

  },
  /**
   * 获取二级分类
   */
  getTwoType() {
    let twoType = {};
    twoType.value = this.data.twoTypeId != -1 ? (this.data.twoTypeId + '').split(',') : [];
    twoType.title = this.data.twoTypeId != -1 ? (this.data.twoTypeName + '').split(',') : [],
      twoType.options = [];
    common.post(url.getTwoType, { id: this.data.oneTypeId }, (res) => {
      for (let i = 0; i < res.length; i++) {
        twoType.options.push({
          title: res[i].name,
          value: res[i].id + '',
          color: "#1296db"
        });
      }
      this.setData({ twoType });
    });

  },

  onOneTypeSelect() {
    var that = this;
    $wuxSelect('#selectOneType').open({
      multiple: false,
      value: that.data.oneType.value,
      options: that.data.oneType.options,
      onConfirm: (value, index, options) => {
        let oneType = that.data.oneType;
        oneType.value = value;
        oneType.title = options[index].title;
        this.setData({
          oneType, oneTypeId: value, twoTypeId: -1
        });
        this.getTwoType();
      },
    });
  },

  onTwoTypeSelect() {
    var that = this;
    $wuxSelect('#selectTwoType').open({
      multiple: true,
      value: that.data.twoType.value,
      options: that.data.twoType.options,
      onConfirm: (value, index, options) => {
        let twoType = that.data.twoType;
        twoType.value = value;
        twoType.title = index.map((n) => options[n].title);
        this.setData({
          twoType
        })
      },
    });
  },

  onVideoPreview(e) {
    const { file, fileList } = e.detail;
    // console.log(e);
    // wx.previewImage({
    //   current: file.url,
    //   urls: fileList.map((n) => n.url),
    // })
  },

  onVideoComplete(e) {
    const { fileList } = e.detail
    wx.hideLoading();
  },

  onVideoChange(e) {
    const { file, fileList } = e.detail
    if (file.status === 'uploading') {
      wx.showLoading({ mask: true, title: '上传中' });
    } else if (file.status === 'done') {
      this.setData({ videoList: fileList });
    }
  },
  onVideoRemove(e) {
    const { file, fileList } = e.detail;
    this.setData({
      videoList: fileList.filter((n) => n.uid !== file.uid),
    })
  },
  onChangeValue(e) {
    let value = e.detail.detail.value;
    this.setData({ content: value });
  },
  onRelease() {
    let params = {};
    let content = this.data.content;
    if (content.length < 5) {
      wx.showModal({
        title: '提示',
        content: '内容字数不能小于5个字',
        success(res) {
        }
      })
      return false;
    }
    let twoTypeId = this.data.twoType.value;
    if (twoTypeId.length == 0) {
      wx.showModal({
        title: '提示',
        content: '请选择要修改的分类',
        success(res) {
        }
      })
      return false;
    }

    let videos = [];
    videos = this.data.videoList.map((n) => {
      if (!n.res) {
        return n.url;
      }
      let data = JSON.parse(n.res.data);
      return data.data.url;
    });

    params.articleId = this.data.id;
    params.oneTypeId = this.data.oneType.value;
    params.content = this.data.content;
    twoTypeId.forEach((element, index) => {
      params['twoTypeId[' + index + ']'] = element;
    });
    if (videos.length > 0)
      params['video'] = videos[0];

    wx.showLoading({ mask: true, title: '更新中' });
    common.post(url.updateArticle, params, (res) => {
      wx.hideLoading();
      wx.showModal({
        title: '提示',
        content: '更新成功',
        success(res) {
          wx.navigateBack({
            delta: 1
          });
        },
      })
    });
  }


})